package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.QrCodeInfoMapper;
import cc.rengu.igas.bomp.common.dao.TermInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.QrCodeInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.TermInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.QrCodeInfo;
import cc.rengu.igas.bomp.common.entity.TermInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.TermAndQrcodeNumQueryRequest;
import cc.rengu.igas.bomp.facade.response.TermAndQrcodeNumQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 商户终端及二维码数量查询
 * Created by xubh on 2020/7/6
 */
public class TermAndQrcodeNumQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        TermAndQrcodeNumQueryRequest termAndQrcodeNumQueryRequest = new TermAndQrcodeNumQueryRequest();
        ConvertUtil.convertOutput(termAndQrcodeNumQueryRequest);
        /* 必填请求参数合法性检查 */
        String userId = termAndQrcodeNumQueryRequest.getUserId();
        String mchntNo = termAndQrcodeNumQueryRequest.getMchntNo();
        String status = termAndQrcodeNumQueryRequest.getStatus();
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(status)) {
            rglog.error("必填性校验失败");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        return termAndQrcodeNumQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        TermAndQrcodeNumQueryRequest termAndQrcodeNumQueryRequest = (TermAndQrcodeNumQueryRequest) request;
        TermAndQrcodeNumQueryResponse termAndQrcodeNumQueryResponse = new TermAndQrcodeNumQueryResponse();
        BeanUtil.beanCopy(termAndQrcodeNumQueryRequest, termAndQrcodeNumQueryResponse);
        BizResponse bizResponse = new BizResponse();

        String instId = termAndQrcodeNumQueryRequest.getHeader().getInstId();
        String mchntNo = termAndQrcodeNumQueryRequest.getMchntNo();
        String status = termAndQrcodeNumQueryRequest.getStatus();

        /*查询直连商户表T_B_DIRECT_MCHNT_BASE_INFO */
        TermInfoMapper termInfoMapper = new TermInfoMapperImpl();
        TermInfo termInf = termInfoMapper.selectBompTermInfoNum(instId, mchntNo, status);
        String termNum = StringUtil.isEmptyOrNull(termInf.getReserve1()) ? "0" : termInf.getReserve1();

        /*查询商户二维码批次明细表 T_B_QRCODE_INFO*/
        QrCodeInfoMapper qrCodeInfoMapper = new QrCodeInfoMapperImpl();
        String codeNum = "0";
        if ("01".equals(status)) {
            status = "00";
            QrCodeInfo qrCodeInfo = qrCodeInfoMapper.selectQrCodeInfoNum(instId, mchntNo, status);
            codeNum = StringUtil.isEmptyOrNull(qrCodeInfo.getRemark3()) ? "0" : qrCodeInfo.getRemark3();
        } else if ("00".equals(status) || "02".equals(status) || "03".equals(status)) {
            status = "01";
            QrCodeInfo qrCodeInfo = qrCodeInfoMapper.selectQrCodeInfoNum(instId, mchntNo, status);
            codeNum = StringUtil.isEmptyOrNull(qrCodeInfo.getRemark3()) ? "0" : qrCodeInfo.getRemark3();
        } else {
            rglog.error("状态超出范围");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        termAndQrcodeNumQueryResponse.setQrcodeNum(codeNum);
        termAndQrcodeNumQueryResponse.setTermNum(termNum);
        termAndQrcodeNumQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        termAndQrcodeNumQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(termAndQrcodeNumQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        TermAndQrcodeNumQueryResponse termAndQrcodeNumQueryResponse = (TermAndQrcodeNumQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(termAndQrcodeNumQueryResponse);
    }

    /**
     * 查询结果转换
     * @param mchntBaseInfo 商户基本信息
     * @param mchntInfoBeans 应答接口对象
     */
    /*private void convMchntInfo(MchntBaseInfo mchntBaseInfo,List<MchntInfoBean> mchntInfoBeans){
        try {
            MchntInfoBean mchntInfoBean = new MchntInfoBean();
            BeanUtil.beanCopy(mchntBaseInfo,mchntInfoBean);
            *//* 获取行业类别名称 *//*
            IndustryInfoMapper industryInfoMapper = new IndustryInfoMapperImpl();
            IndustryInfo industryInfo = industryInfoMapper.selectIndustryInfoByIndustryType(mchntBaseInfo.getRemark1());
            if (null != industryInfo) {
                mchntInfoBean.setIndustryName(industryInfo.getIndustryName());
            }
            *//* 获取微信商户信息 *//*
            WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
            WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(),mchntBaseInfo.getMchntNo());
            if (null != wechatMchntInfo){
                mchntInfoBean.setWechatExpandStatus(wechatMchntInfo.getWechatEntryFlag());
                mchntInfoBean.setRealNameFlag(wechatMchntInfo.getRealNameFlag());
                mchntInfoBean.setRealNameApplyNo(wechatMchntInfo.getRealNameApplyNo());
            }
            *//* 获取支付宝商户信息 *//*
            AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
            AlipayMchntInfo alipayMchntInfo = alipayMchntInfoMapper.selectAlipayMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(),mchntBaseInfo.getMchntNo());
            if (null != alipayMchntInfo){
                mchntInfoBean.setAlipayExpandStatus(alipayMchntInfo.getAlipayEntryFlag());
            }
            *//* 获取银联商户信息 *//*
            CupsMchntInfoMapper cupsMchntInfoMapper = new CupsMchntInfoMapperImpl();
            CupsMchntInfo cupsMchntInfo = cupsMchntInfoMapper.selectCupsMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(),mchntBaseInfo.getMchntNo());
            if (null != cupsMchntInfo){
                mchntInfoBean.setCupsReportStatus(cupsMchntInfo.getCupsReportStatus());
            }
            *//* 获取代理机构名称 *//*
            if (null != mchntBaseInfo.getAgentMchntNo() && !mchntBaseInfo.getAgentMchntNo().isEmpty()) {
                AgentInfoMapper agentInfoMapper = new AgentInfoMapperImpl();
                AgentInfo agentInfo = agentInfoMapper.selectAgentInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getAgentMchntNo());
                if (null != agentInfo){
                    mchntInfoBean.setAgentMchntName(agentInfo.getAgentName());
                }
            }
            *//* 获取签约机构名称 *//*
            OrganizationMapper organizationMapper = new OrganizationMapperImpl();
            Organization organization = organizationMapper.selectOrganizationByPrimaryKey(mchntBaseInfo.getInstId(),mchntBaseInfo.getSignInstId());
            if (null != organization){
                mchntInfoBean.setSignInstName(organization.getOrgName());
            }
            mchntInfoBeans.add(mchntInfoBean);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream,true));
            rglog.error("商户列表查询详细信息转换失败:{}",byteArrayOutputStream.toString());
        }
    }*/
}
